﻿//************************************************************ 
//Original Code by 
//TaskbarNotifier, a skinnable MSN Messenger By John O'Byrne
//http://www.codeproject.com/KB/miscctrl/taskbarnotifier.aspx
//************************************************************ 
//Modify by Fernando Cerqueira - fenandocerqueira@msn.com
//Simplify usage
//Multiples windows
//************************************************************ 
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Taskbar
{
    #region TaskbarNotifier Enums
    /// <summary>
    /// List of the different popup animation status
    /// </summary>
    public enum TaskbarStates
    {
        hidden = 0,
        appearing = 1,
        visible = 2,
        disappearing = 3
    }
    #endregion

    public class Notifier : System.Windows.Forms.Form
    {
        #region TaskbarNotifier static Members

        protected static Dictionary<string, Notifier> _TaskbarNotifiers = new Dictionary<string, Notifier>();
        protected static string _Title = "";
        protected static bool showGradientebackgroud = false;
        protected static Color backgroudColor = SystemColors.Control;
        protected static Color gradienteStartColor = Color.FromArgb(150, 206, 231);
        protected static Color gradienteEndColor = Color.FromArgb(255, 255, 255);
        protected static Color normalTitleColor = Color.FromArgb(255, 0, 0);
        protected static Color hoverTitleColor = Color.FromArgb(255, 0, 0);
        protected static Color normalContentColor = Color.FromArgb(0, 0, 0);
        protected static Color hoverContentColor = Color.FromArgb(0, 0, 0x66);
        protected static Font normalTitleFont = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel);
        protected static Font hoverTitleFont = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);
        protected static Font normalContentFont = new Font("Arial", 11, FontStyle.Regular, GraphicsUnit.Pixel);
        protected static Font hoverContentFont = new Font("Arial", 11, FontStyle.Regular, GraphicsUnit.Pixel);
        protected static int notifierMaxWidth = 0;
        protected static bool bKeepVisibleOnMouseOver = true;
        protected static bool bReShowOnMouseOver = true;
        protected static int _TimeToShow = 600;
        protected static int _TimeToStay = 5000;
        protected static int _TimeToHide = 700;
        protected static int CountShow = 0;

        #endregion    

        #region TaskbarNotifier Protected Members


        protected string IDNotifier = null;
        protected int CurrentCount = 0;
        protected Bitmap BackgroundBitmap = null;
        protected Rectangle WorkAreaRectangle;
        protected Point CloseBitmapLocation;
        protected Size CloseBitmapSize;
        protected Bitmap CloseBitmap = null;
        protected System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        protected TaskbarStates taskbarState = TaskbarStates.hidden;
        protected string titleText;
        protected string contentText;
        protected int nShowEvents;
        protected int nHideEvents;
        protected int nVisibleEvents;
        protected int nIncrementShow;
        protected int nIncrementHide;
        protected bool bIsMouseOverPopup = false;
        protected bool bIsMouseOverClose = false;
        protected bool bIsMouseOverContent = false;
        protected bool bIsMouseOverTitle = false;
        protected bool bIsMouseDown = false;
        protected bool bIsLoad = false;
        protected Rectangle TitleRectangle;
        protected Rectangle ContentRectangle;
        protected bool TitleClickable = true;
        protected bool ContentClickable = true;
        protected bool CloseClickable = true;
        protected bool EnableSelectionRectangle = true;
        protected int _heightToolbatWindows = 35; 

        #endregion

        #region TaskbarNotifier Public events

        public event EventHandler CloseClick = null;
        public event EventHandler TitleClick = null;
        public event EventHandler ContentClick = null;
        
        #endregion

        #region TaskbarNotifier Constructor
        /// <summary>
        /// The Constructor for TaskbarNotifier
        /// </summary>
        public Notifier()
        {
            // Window Style
            bIsLoad = true;
            FormBorderStyle = FormBorderStyle.FixedSingle;
            WindowState = FormWindowState.Normal;
            Opacity = 0;
            base.Show();
            base.Hide();
            Opacity = 1;
            ShowInTaskbar = false;
            TopMost = true;
            MaximizeBox = false;
            MinimizeBox = false;
            ControlBox = false;
            timer.Enabled = false;
            timer.Tick += new EventHandler(OnTimer);
            bIsLoad = false;

            CloseBitmap = new Bitmap( Properties.Resources.closeNotifier);
            CloseBitmap.MakeTransparent(Color.FromArgb(255, 0, 255));
            CloseBitmapSize = new Size(CloseBitmap.Width / 3, CloseBitmap.Height);
            CloseBitmapLocation = new Point(0, 5);

        }

        #endregion

        #region TaskbarNotifier static Properties

        public static int TimeToShow { get { return _TimeToShow; } set { _TimeToShow = value; } }

        public static int TimeToStay { get { return _TimeToStay; } set { _TimeToStay = value; } }

        public static int TimeToHide { get { return _TimeToHide; } set { _TimeToHide = value; } }

        public static string Title { get { return _Title; } set { _Title = value; } }

        /// <summary>
        /// Get/Set the Gradiente Start Color
        /// </summary>
        public static Color GradienteStartColor
        {
            get
            {
                return gradienteStartColor;
            }
            set
            {
                gradienteStartColor = value;
            }
        }

        /// <summary>
        /// Get/Set the Gradiente End Color
        /// </summary>
        public static Color GradienteEndColor
        {
            get
            {
                return gradienteEndColor;
            }
            set
            {
                gradienteEndColor = value;
            }
        }

        /// <summary>
        /// Get/Set the Show Gradiente backgroud
        /// </summary>
        public static bool ShowGradientebackgroud
        {
            get
            {
                return showGradientebackgroud;
            }
            set
            {
                showGradientebackgroud = value;
            }
        }

        /// <summary>
        /// Get/Set the MaxWidth
        /// </summary>
        /// <remarks>
        /// Zero value MaxWidth = Max. line Length 
        /// </remarks>
        public static int NotifierMaxWidth
        {
            get
            {
                return notifierMaxWidth;
            }
            set
            {
                notifierMaxWidth = value;
            }
        }

        /// <summary>
        /// Get/Set the Normal Title Color
        /// </summary>
        public static Color NormalTitleColor
        {
            get
            {
                return normalTitleColor;
            }
            set
            {
                normalTitleColor = value;
            }
        }

        /// <summary>
        /// Get/Set the Hover Title Color
        /// </summary>
        public static Color HoverTitleColor
        {
            get
            {
                return hoverTitleColor;
            }
            set
            {
                hoverTitleColor = value;
            }
        }

        /// <summary>
        /// Get/Set the Normal Content Color
        /// </summary>
        public static Color NormalContentColor
        {
            get
            {
                return normalContentColor;
            }
            set
            {
                normalContentColor = value;
            }
        }

        /// <summary>
        /// Get/Set the Hover Content Color
        /// </summary>
        public static Color HoverContentColor
        {
            get
            {
                return hoverContentColor;
            }
            set
            {
                hoverContentColor = value;
                //Refresh();
            }
        }

        /// <summary>
        /// Get/Set the Normal Title Font
        /// </summary>
        public static Font NormalTitleFont
        {
            get
            {
                return normalTitleFont;
            }
            set
            {
                normalTitleFont = value;
                //Refresh();
            }
        }

        /// <summary>
        /// Get/Set the Hover Title Font
        /// </summary>
        public static Font HoverTitleFont
        {
            get
            {
                return hoverTitleFont;
            }
            set
            {
                hoverTitleFont = value;
            }
        }

        /// <summary>
        /// Get/Set the Normal Content Font
        /// </summary>
        public static Font NormalContentFont
        {
            get
            {
                return normalContentFont;
            }
            set
            {
                normalContentFont = value;
            }
        }

        /// <summary>
        /// Get/Set the Hover Content Font
        /// </summary>
        public static Font HoverContentFont
        {
            get
            {
                return hoverContentFont;
            }
            set
            {
                hoverContentFont = value;
            }
        }

        /// <summary>
        /// Indicates if the popup should remain visible when the mouse pointer is over it.
        /// </summary>
        public static bool KeepVisibleOnMousOver
        {
            get
            {
                return bKeepVisibleOnMouseOver;
            }
            set
            {
                bKeepVisibleOnMouseOver = value;
            }
        }

        /// <summary>
        /// Indicates if the popup should appear again when mouse moves over it while it's disappearing.
        /// </summary>
        public static bool ReShowOnMouseOver
        {
            get
            {
                return bReShowOnMouseOver;
            }
            set
            {
                bReShowOnMouseOver = value;
            }
        }

        #endregion

        #region TaskbarNotifier Properties

        /// <summary>
        /// Get the current TaskbarState (hidden, showing, visible, hiding)
        /// </summary>
        /// 
        public TaskbarStates TaskbarState
        {
            get
            {
                return taskbarState;
            }
        }
        /// <summary>
        /// Get/Set the popup Title Text
        /// </summary>
        public string TitleText
        {
            get
            {
                return titleText;
            }
            set
            {
                titleText = value;
            }
        }
        internal int StartYpos { get; set; }

        public int heightToolbatWindows 
        {
            get
            {
                return _heightToolbatWindows;
            }
            set
            {
                _heightToolbatWindows = value;
            }
        }
        /// <summary>
        /// Get/Set the popup Content Text
        /// </summary>
        public string ContentText
        {
            get
            {
                return contentText;
            }
            set
            {
                contentText = value;
            }
        }


        #endregion

        #region TaskbarNotifier Public Methods

        [DllImport("user32.dll")]
        private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

        /// <summary>
        /// Displays the popup for a certain amount of time
        /// </summary>
        /// <param name="strTitle">The string which will be shown as the title of the popup</param>
        /// <param name="strContent">The string which will be shown as the content of the popup</param>
        /// <param name="nTimeToShow">Duration of the showing animation (in milliseconds)</param>
        /// <param name="nTimeToStay">Duration of the visible state before collapsing (in milliseconds)</param>
        /// <param name="nTimeToHide">Duration of the hiding animation (in milliseconds)</param>
        /// <returns>Nothing</returns>
        /// 
        public static string Show(string strContent)
        {
            return Show(_Title, strContent);
        }

        public static string Show(string strTitle, string strContent)
        {
            CountShow++;
            string ID = Guid.NewGuid().ToString();
            Notifier newtask = new Notifier();
            newtask.IDNotifier = ID;
            _TaskbarNotifiers.Add(ID, newtask);
            newtask.internalShow(strTitle, strContent);
            return ID;
        }
     
        private void internalShow(string strTitle, string strContent)
        {
            WorkAreaRectangle = Screen.PrimaryScreen.WorkingArea;
            this.titleText = strTitle;
            this.contentText = strContent;
            nVisibleEvents = TimeToStay;

            Graphics grfx = CreateGraphics();
            SizeF sizefTitle = grfx.MeasureString(titleText, hoverTitleFont);
            string[] Lines = contentText.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            SizeF sizefContent = new SizeF();
            int maxwidth = NotifierMaxWidth + (CloseBitmap.Width / 3);
            foreach (string Line in Lines)
            {
                SizeF szline = grfx.MeasureString(Line, hoverContentFont);
                sizefContent.Width = (int)Math.Max(sizefContent.Width, szline.Width);
                sizefContent.Height += szline.Height;
                if (szline.Width > maxwidth && NotifierMaxWidth != 0)
                {
                    float adjus = szline.Width / maxwidth;
                    sizefContent.Height += szline.Height * adjus;
                }
            }
            if (NotifierMaxWidth == 0)
            {
                maxwidth = (int)Math.Max(sizefTitle.Width, sizefContent.Width) + 10 + CloseBitmap.Width / 3;
            }
            else
            {
                maxwidth += (CloseBitmap.Width / 3);
            }
            int maxheight = (int)sizefTitle.Height + (int)sizefContent.Height + 10;
            this.Size = new Size(maxwidth,maxheight);
            BackgroundBitmap = new Bitmap(maxwidth, maxheight);
            int CurrentFator = 0;
            foreach (KeyValuePair<string, Notifier> kvp in Notifier._TaskbarNotifiers)
            {
                if (kvp.Value.IDNotifier != this.IDNotifier)
                {
                    CurrentFator += kvp.Value.StartYpos;
                }
            }
            CurrentFator += _heightToolbatWindows;
            _TaskbarNotifiers[this.IDNotifier].StartYpos = BackgroundBitmap.Height + 5;
            if (CurrentFator != 0)
            {
                this.Opacity = 0;
            }


            TitleRectangle = new Rectangle(5, 2, BackgroundBitmap.Width - (CloseBitmap.Width / 3) - 5, (int)sizefTitle.Height);
            ContentRectangle = new Rectangle(5, (int)sizefTitle.Height + 5, BackgroundBitmap.Width - 5 - (CloseBitmap.Width / 3), (int)sizefContent.Height);

            if (ShowGradientebackgroud)
            {
                Graphics Gfx = Graphics.FromImage(BackgroundBitmap);
                LinearGradientBrush brGY = new LinearGradientBrush(
                new Rectangle(0, 0, BackgroundBitmap.Width, BackgroundBitmap.Height),
                gradienteStartColor,
                gradienteEndColor,
                90,
                false);
                Gfx.FillRectangle(brGY, new Rectangle(0, 0, BackgroundBitmap.Width, BackgroundBitmap.Height));
                Gfx.Dispose();
            }
            else
            {
                for (int Xcount = 0; Xcount < BackgroundBitmap.Width; Xcount++)
                {
                    for (int Ycount = 0; Ycount < BackgroundBitmap.Height; Ycount++)
                    {
                        BackgroundBitmap.SetPixel(Xcount, Ycount, backgroudColor);
                    }
                }
            }

            // We calculate the pixel increment and the timer value for the showing animation
            int nEvents;
            if (TimeToShow > 10)
            {
                nEvents = Math.Min((TimeToShow / 10), BackgroundBitmap.Height);
                nShowEvents = TimeToShow / nEvents;
                nIncrementShow = BackgroundBitmap.Height / nEvents;
            }
            else
            {
                nShowEvents = 10;
                nIncrementShow = BackgroundBitmap.Height;
            }

            // We calculate the pixel increment and the timer value for the hiding animation
            if (TimeToHide > 10)
            {
                nEvents = Math.Min((TimeToHide / 10), BackgroundBitmap.Height);
                nHideEvents = TimeToHide / nEvents;
                nIncrementHide = BackgroundBitmap.Height / nEvents;
            }
            else
            {
                nHideEvents = 10;
                nIncrementHide = BackgroundBitmap.Height;
            }

            switch (taskbarState)
            {
                case TaskbarStates.hidden:
                    taskbarState = TaskbarStates.appearing;
                    SetBounds(WorkAreaRectangle.Right - BackgroundBitmap.Width - 17, WorkAreaRectangle.Height - 1 - CurrentFator , BackgroundBitmap.Width, 0);
                    timer.Interval = nShowEvents;
                    timer.Start();
                    // We Show the popup without stealing focus
                    ShowWindow(this.Handle, 4);
                    break;
                case TaskbarStates.appearing:
                    Refresh();
                    break;

                case TaskbarStates.visible:
                    timer.Stop();
                    timer.Interval = nVisibleEvents;
                    timer.Start();
                    Refresh();
                    break;

                case TaskbarStates.disappearing:
                    timer.Stop();
                    taskbarState = TaskbarStates.visible;
                    SetBounds(WorkAreaRectangle.Right - BackgroundBitmap.Width - 17, WorkAreaRectangle.Bottom - BackgroundBitmap.Height - 1, BackgroundBitmap.Width, BackgroundBitmap.Height);
                    timer.Interval = nHideEvents;
                    timer.Start();
                    Refresh();
                    break;
            }
        }

        /// <summary>
        /// Hides the popup
        /// </summary>
        /// <returns>Nothing</returns>
        /// 
        private new void Hide()
        {
            if (taskbarState != TaskbarStates.hidden)
            {
                timer.Stop();
                taskbarState = TaskbarStates.hidden;
            }
            base.Hide();
            this.Close();
            if (_TaskbarNotifiers.ContainsKey(IDNotifier))
            {
                _TaskbarNotifiers.Remove(IDNotifier);
            }
            if (CountShow > 0)
            {
                CountShow--;
            }
        }

        public static void HideAll()
        {
            List<string> keydel = new List<string>();
            foreach (KeyValuePair<string,Notifier> kvp in _TaskbarNotifiers)
            {
                if (kvp.Value.taskbarState != TaskbarStates.hidden)
                {
                    keydel.Add(kvp.Key);
                }
                if (CountShow > 0)
                {
                    CountShow--;
                }
            }
            foreach (string item in keydel)
            {
                _TaskbarNotifiers[item].timer.Stop();
                _TaskbarNotifiers[item].taskbarState = TaskbarStates.hidden;
                _TaskbarNotifiers[item].Hide();
                _TaskbarNotifiers[item].Close();
                _TaskbarNotifiers.Remove(item);
            }
            CountShow = 0;
            _TaskbarNotifiers.Clear();
        }

        public static void Hide(string ID)
        {
            if (_TaskbarNotifiers.ContainsKey(ID))
            {
                if (_TaskbarNotifiers[ID].taskbarState != TaskbarStates.hidden)
                {
                    _TaskbarNotifiers[ID].timer.Stop();
                    _TaskbarNotifiers[ID].taskbarState = TaskbarStates.hidden;
                    _TaskbarNotifiers[ID].Hide();
                }
            }
        }

        public static TaskbarStates GetTaskbarState(string ID)
        {
            if (_TaskbarNotifiers.ContainsKey(ID))
            {

                return _TaskbarNotifiers[ID].taskbarState;
            }

            return TaskbarStates.hidden;
        }

        #endregion

        #region TaskbarNotifier Protected Methods

        protected void DrawCloseButton(Graphics grfx)
        {
            if (CloseBitmap != null)
            {
                CloseBitmapLocation.X = BackgroundBitmap.Width - CloseBitmapSize.Width - 5;
                CloseBitmapLocation.Y = 5;
                Rectangle rectDest = new Rectangle(CloseBitmapLocation, CloseBitmapSize);
                Rectangle rectSrc;

                if (bIsMouseOverClose)
                {
                    if (bIsMouseDown)
                        rectSrc = new Rectangle(new Point(CloseBitmapSize.Width * 2, 0), CloseBitmapSize);
                    else
                        rectSrc = new Rectangle(new Point(0, 0), CloseBitmapSize);
                }
                else
                    rectSrc = new Rectangle(new Point(CloseBitmapSize.Width, 0), CloseBitmapSize);


                grfx.DrawImage(CloseBitmap, rectDest, rectSrc, GraphicsUnit.Pixel);
            }
        }

        protected void DrawText(Graphics grfx)
        {
            if (titleText != null && titleText.Length != 0)
            {
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                sf.FormatFlags = StringFormatFlags.NoWrap;
                sf.Trimming = StringTrimming.EllipsisCharacter;				// Added Rev 002
                if (bIsMouseOverTitle)
                    grfx.DrawString(titleText, hoverTitleFont, new SolidBrush(hoverTitleColor), TitleRectangle, sf);
                else
                    grfx.DrawString(titleText, normalTitleFont, new SolidBrush(normalTitleColor), TitleRectangle, sf);
            }

            if (contentText != null && contentText.Length != 0)
            {
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
                sf.Trimming = StringTrimming.Word;							// Added Rev 002

                if (bIsMouseOverContent)
                {
                    grfx.DrawString(contentText, hoverContentFont, new SolidBrush(hoverContentColor), ContentRectangle, sf);
                    if (EnableSelectionRectangle)
                        ControlPaint.DrawBorder3D(grfx, ContentRectangle /*RealContentRectangle*/, Border3DStyle.Etched, Border3DSide.Top | Border3DSide.Bottom | Border3DSide.Left | Border3DSide.Right);

                }
                else
                    grfx.DrawString(contentText, normalContentFont, new SolidBrush(normalContentColor), ContentRectangle, sf);
            }
        }

        #endregion

        #region TaskbarNotifier Events Overrides

        protected void OnTimer(Object obj, EventArgs ea)
        {
            switch (taskbarState)
            {
                case TaskbarStates.appearing:
                    if (this.ClientRectangle.Height < BackgroundBitmap.Height)
                    {
                        SetBounds(Left, Top - nIncrementShow, Width, Height + nIncrementShow);
                        if (CurrentCount != 0)
                        {
                            if (this.Opacity < 1)
                            {
                                this.Opacity += 0.1;
                            }
                        }
                    }
                    else
                    {
                        timer.Stop();
                        this.Opacity = 1;
                        Height = BackgroundBitmap.Height;
                        timer.Interval = nVisibleEvents;
                        taskbarState = TaskbarStates.visible;
                        timer.Start();
                    }
                    break;
                case TaskbarStates.visible:
                    timer.Stop();
                    timer.Interval = nHideEvents;
                    if ((bKeepVisibleOnMouseOver && !bIsMouseOverPopup) || (!bKeepVisibleOnMouseOver))
                    {
                        taskbarState = TaskbarStates.disappearing;
                    }
                    timer.Start();
                    break;

                case TaskbarStates.disappearing:
                    if (bReShowOnMouseOver && bIsMouseOverPopup)
                    {
                        this.TopMost = false;
                        taskbarState = TaskbarStates.appearing;
                    }
                    else
                    {
                        this.TopMost = false;
                        if (Top < WorkAreaRectangle.Bottom)
                        {
                            SetBounds(Left, Top + nIncrementHide, Width, Height);
                        }
                        else
                            Hide();
                    }
                    break;
            }

        }

        protected override void OnMouseEnter(EventArgs ea)
        {
            base.OnMouseEnter(ea);
            bIsMouseOverPopup = true;
            Refresh();
        }

        protected override void OnMouseLeave(EventArgs ea)
        {
            base.OnMouseLeave(ea);
            bIsMouseOverPopup = false;
            bIsMouseOverClose = false;
            bIsMouseOverTitle = false;
            bIsMouseOverContent = false;
            Refresh();
        }

        protected override void OnMouseMove(MouseEventArgs mea)
        {
            base.OnMouseMove(mea);

            bool bContentModified = false;

            if ((mea.X > CloseBitmapLocation.X) && (mea.X < CloseBitmapLocation.X + CloseBitmapSize.Width) && (mea.Y > CloseBitmapLocation.Y) && (mea.Y < CloseBitmapLocation.Y + CloseBitmapSize.Height) && CloseClickable)
            {
                if (!bIsMouseOverClose)
                {
                    bIsMouseOverClose = true;
                    bIsMouseOverTitle = false;
                    bIsMouseOverContent = false;
                    Cursor = Cursors.Hand;
                    bContentModified = true;
                }
            }
            else if (ContentRectangle.Contains(new Point(mea.X, mea.Y)) && ContentClickable)
            {
                if (!bIsMouseOverContent)
                {
                    bIsMouseOverClose = false;
                    bIsMouseOverTitle = false;
                    bIsMouseOverContent = true;
                    Cursor = Cursors.Hand;
                    bContentModified = true;
                }
            }
            else if (TitleRectangle.Contains(new Point(mea.X, mea.Y)) && TitleClickable)
            {
                if (!bIsMouseOverTitle)
                {
                    bIsMouseOverClose = false;
                    bIsMouseOverTitle = true;
                    bIsMouseOverContent = false;
                    Cursor = Cursors.Hand;
                    bContentModified = true;
                }
            }
            else
            {
                if (bIsMouseOverClose || bIsMouseOverTitle || bIsMouseOverContent)
                    bContentModified = true;

                bIsMouseOverClose = false;
                bIsMouseOverTitle = false;
                bIsMouseOverContent = false;
                Cursor = Cursors.Default;
            }

            if (bContentModified)
                Refresh();
        }

        protected override void OnMouseDown(MouseEventArgs mea)
        {
            base.OnMouseDown(mea);
            bIsMouseDown = true;

            if (bIsMouseOverClose)
                Refresh();
        }

        protected override void OnMouseUp(MouseEventArgs mea)
        {
            base.OnMouseUp(mea);
            bIsMouseDown = false;

            if (bIsMouseOverClose)
            {
                Hide();

                if (CloseClick != null)
                    CloseClick(this, new EventArgs());
            }
            else if (bIsMouseOverTitle)
            {
                if (TitleClick != null)
                    TitleClick(this, new EventArgs());
            }
            else if (bIsMouseOverContent)
            {
                if (ContentClick != null)
                    ContentClick(this, new EventArgs());
            }
        }

        protected override void OnPaintBackground(PaintEventArgs pea)
        {
            if (bIsLoad || BackgroundBitmap == null) return;

            Graphics grfx = pea.Graphics;
            grfx.PageUnit = GraphicsUnit.Pixel;

            Graphics offScreenGraphics;
            Bitmap offscreenBitmap;

            offscreenBitmap = new Bitmap(BackgroundBitmap.Width, BackgroundBitmap.Height);
            offScreenGraphics = Graphics.FromImage(offscreenBitmap);

            if (BackgroundBitmap != null)
            {
                offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0, BackgroundBitmap.Width, BackgroundBitmap.Height);
            }

            DrawCloseButton(offScreenGraphics);
            DrawText(offScreenGraphics);
            grfx.DrawImage(offscreenBitmap, 0, 0);
            grfx.Dispose();
        }

        #endregion
    }
}
